<?php

namespace utils\oss;

use think\facade\Log;

class QiniuOssService extends OssService
{
    /**
     * 执行上传操作
     * @param $tmpInfo
     * @return string
     * @throws \Exception
     */
    public static function upload($tmpInfo): string
    {
        try {
            // 构建参数
            $accessKey = config('rds.qiniu_oss.access_key');
            $secretKey = config('rds.qiniu_oss.secret_key');
            $bucket = config('rds.qiniu_oss.bucket');
            $domain = config('rds.qiniu_oss.domain');
            // 初始化
            $auth = new \Qiniu\Auth($accessKey, $secretKey);
            $token = $auth->uploadToken($bucket);
            $uploadManager = new \Qiniu\Storage\UploadManager();
            // 生成路径和名称
            $pathName = Parent::setPath('_qiniu_', $tmpInfo);
            // 执行上传
            $ret = $uploadManager->putFile($token, $pathName, $tmpInfo['tmp_name']);
            // 完整URL
            $url = $domain . '/' . $ret[0]['key'];
        } catch (\Exception $e) {
            Log::record($e->getMessage());
            throw new \Exception('七牛云上传失败');
        }
        return $url;
    }
}